<template>
  <van-icon :color="isCollected ? '#f60' : '#777'" :name="isCollected ? 'star' : 'star-o'" @click="onCollect" />
</template>

<script>
import { addCollect, deleteCollect } from '@/api/article'
export default {
  name: '',
  props: {
    isCollected: {
      type: Boolean,
      required: true,
    },
    articleId: {
      type: [String, Number, Object],
      required: true,
    },
  },
  data() {
    return {}
  },
  methods: {
    async onCollect() {
      try {
        if (this.isCollected) {
          // 取消收藏
          await deleteCollect(this.articleId)
        } else {
          // 添加收藏
          await addCollect(this.articleId)
        }

        this.$emit("update:isCollected",!this.isCollected);

        this.$toast.success(!this.isCollected?'收藏成功':'取消收藏')
      } catch (err) {
          console.log(err);
          this.$toast("收藏操作失败")
      }
    },
  },
  created() {},
}
</script>

<style lang="" scoped>
</style>